package com.jht.bletool2kotlin.activity.homepage


import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import com.jht.bletool2kotlin.R
import com.jht.bletool2kotlin.activity.BaseActivity
import com.jht.bletool2kotlin.activity.blepage.BlePageActivity
import com.jht.bletool2kotlin.activity.homepage.adapter.HomeViewPageAdapter
import com.jht.bletool2kotlin.activity.homepage.fragment.device.DeviceFragment
import com.jht.bletool2kotlin.activity.homepage.fragment.device.bean.BLEDevice
import com.jht.bletool2kotlin.activity.homepage.fragment.simulation.SimulationFragment
import com.jht.bletool2kotlin.activity.setting.SettingActivity
import kotlinx.android.synthetic.main.activity_home.*
import kotlinx.android.synthetic.main.common_layout_toolbar.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

private const val TAG = "HomeActivity"

class HomeActivity : BaseActivity(), View.OnClickListener, IHome.UI {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun getLayoutID(): Int {
        return R.layout.activity_home
    }

    override fun initView() {
        iv_menu.setOnClickListener(this)
        progress_view.visibility = View.INVISIBLE
        initViewPage()
    }

    private fun initViewPage() {
        val deviceFragment = DeviceFragment.newInstance()
        val simulationFragment = SimulationFragment.newInstance()
        vp_main.adapter = HomeViewPageAdapter(
            supportFragmentManager,
            listOf(deviceFragment, simulationFragment),
            listOf("蓝牙设备", "模拟设备")
        )
        tab_layout.setupWithViewPager(vp_main)
    }

    override fun onClick(v: View?) {
        when (v?.id){
            R.id.iv_menu -> {
                goSettingActivityPre()
            }
        }
    }

    private fun goSettingActivityPre() {
        hideProgressView(true)
        EventBus.getDefault().post(DeviceFragment.HomeActivityEvent())
    }

    private fun goSettingActivity() {
        val intent = Intent(this,SettingActivity::class.java)
        startActivity(intent)
    }

    override fun onStart() {
        super.onStart()
        EventBus.getDefault().register(this)
    }

    override fun onStop() {
        super.onStop()
        EventBus.getDefault().unregister(this)
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    fun changeUI(uiEvent: UIEvent) {
        when(uiEvent.type){
            Type.OPEN_PROCESS_BAR -> hideProgressView(false)
            Type.CLOSE_PROCESS_BAR -> hideProgressView(true)
            Type.GO_SETTING_ACTIVITY -> goSettingActivity()
        }
    }

    private fun hideProgressView(visible:Boolean){
        when(visible){
            true -> progress_view.visibility = View.GONE
            false -> progress_view.visibility = View.VISIBLE
        }
    }

    class UIEvent(val type:Type)


    enum class Type(){
        CLOSE_PROCESS_BAR,OPEN_PROCESS_BAR,GO_SETTING_ACTIVITY
    }

}